<?php
/*
 * @@Copyright (C), 2021-2022: 恒盛时代
 * @Author: zhouzhou
 * @Date: 2021/11/30
 * @Description: 聊天置顶表
 */

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

class ImChatTop extends BaseModel
{
    protected $table = 'im_chat_top';

    /**
     * @desc 单聊置顶取消置顶 top 1置顶 0取消置顶
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function addTopChat($input, $userId = 0)
    {
        $chatTop = $this->where(['chat_key_id'=>$input['chat_key_id']])->first();

        // 查询好友是否好友关系
        $imUserFriend = new ImUserFriend();
        $imUserFriend = $imUserFriend->where(['user_id'=>$userId, 'other_uid'=>$input['other_uid']])->first();

        if (!$imUserFriend)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        // 置顶
        if ($input['top'] == 1)
        {
            if (!$chatTop)
            {
                $this->chat_key_id = $input['chat_key_id'];
                $this->user_id = $userId;
                $this->type = 1;
                $this->status = 1;
                $this->save();

                $imUserFriend->top = 1;
                $imUserFriend->save();

                return ['code' => 0, 'msg' => '置顶成功'];
            }
            else
            {
                $chatTop->status = 1;
                $chatTop->save();

                $imUserFriend->top = 1;
                $imUserFriend->save();

                return ['code' => 0, 'msg' => '置顶成功'];
            }
        }
        else
        {
            // 取消置顶
            if (!$chatTop)
            {
                $this->chat_key_id = $input['chat_key_id'];
                $this->user_id = $userId;
                $this->type = 1;
                $this->status = 0;
                $this->save();

                $imUserFriend->top = 0;
                $imUserFriend->save();

                return ['code' => 0, 'msg' => '取消成功'];
            }
            else
            {
                $chatTop->status = 0;
                $chatTop->save();

                $imUserFriend->top = 0;
                $imUserFriend->save();

                return ['code' => 0, 'msg' => '取消成功'];
            }
        }
    }

    /**
     * @desc 群聊置顶取消置顶 top 1置顶 0取消置顶
     * @param array $input 传过来数组对象
     * @param int $userId 用户id
     * @return array|int[]
     * @author zhouzhou
     * @time 2021/11/23 14:58
     */
    public function addTopGroupChat($input, $userId = 0)
    {
        $chatTop = $this->where(['chat_key_id'=>$input['chat_key_id']])->first();

        // 查询是否在这个群里
        $imGroupUser = new ImGroupUser();
        $imGroupUser = $imGroupUser->where(['im_gid' => $input['im_gid'], 'user_id' => $userId])->whereIn('status', [0,3])->first();
        if (!$imGroupUser)
        {
            return ['code' => 1, 'msg' => '系统错误'];
        }

        // 置顶
        if ($input['top'] == 1)
        {
            if (!$chatTop)
            {
                $this->chat_key_id = $input['chat_key_id'];
                $this->user_id = $userId;
                $this->type = 2;
                $this->status = 1;
                $this->save();

                $imGroupUser->top = 1;
                $imGroupUser->save();

                return ['code' => 0, 'msg' => '置顶成功'];
            }
            else
            {
                $chatTop->status = 1;
                $chatTop->save();

                $imGroupUser->top = 1;
                $imGroupUser->save();

                return ['code' => 0, 'msg' => '置顶成功'];
            }
        }
        else
        {
            // 取消置顶
            if (!$chatTop)
            {
                $this->chat_key_id = $input['chat_key_id'];
                $this->user_id = $userId;
                $this->type = 2;
                $this->status = 0;
                $this->save();

                $imGroupUser->top = 0;
                $imGroupUser->save();

                return ['code' => 0, 'msg' => '取消成功'];
            }
            else
            {
                $chatTop->status = 0;
                $chatTop->save();

                $imGroupUser->top = 0;
                $imGroupUser->save();

                return ['code' => 0, 'msg' => '取消成功'];
            }
        }
    }



}
